package com.sxy;

import com.sxy.mapper.StuMapper;
import com.sxy.pojo.Stu;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class MybatisDemo04 {
    public static void main(String[] args) throws Exception {
        //接收参数
        String name = "a";
        int stu_class = 1;
        //处理参数
        name = "%" + name + "%";
        //封装成对象
        Stu stu = new Stu();
        stu.setName(name);

        //封装成Map集合
//        Map<String,String> map = new HashMap<>();
//        map.put("name",name);

        //1.加载mybatis的核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取对应的SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.执行sql语句
//        List<User> list = sqlSession.selectList("test.selectAll");
        //获取对应的UserMapper接口的代理对象,并执行sql
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
//        List<Stu> stus = mapper.selectByCondition(name, 1);
        //通过封装成对象来传递参数
        List<Stu> stus = mapper.selectByConditionSingle(stu);
        //通过Map集合来传递参数
//        List<Stu> stus = mapper.selectByConditionSingle(map);
        //4.处理结果
        System.out.println(stus);

        //5.释放资源 只需释放SqlSession即可
        sqlSession.close();
    }
}
